<?php
	error_reporting(E_ALL || ~E_NOTICE);
    $deresult=file_get_contents('php://input');
//    $string = "\n deresult:".$deresult."\n";
//    file_put_contents("test.log",$string, FILE_APPEND);	
    $indexA=strpos($deresult,'{');   
	$indexB=strpos($deresult,'}');	
	while($indexA && $indexB)
    {		    
		$deresultJson=substr($deresult,$indexA,$indexB-$indexA+1);
		$string = "\n deresultJson:".$deresultJson."\n";
//       file_put_contents("test.log",$string, FILE_APPEND);
//        $deresult=substr($deresult,$indexA+1);
//		file_put_contents("test.log","deresult...".$deresult, FILE_APPEND);
		$deresultArr=json_decode($deresultJson,true);
//	    $string = "\n device_id:".$deresultArr["device_id"]."trans_id:".$deresultArr["trans_id"]."cmd_result:".$deresultArr["cmd_result"]."detime:".$deresultArr["time"]."\n";
//        file_put_contents("test.log", $string, FILE_APPEND);
	    $deval=$deresultArr["device_id"];
	    $detrans=$deresultArr["trans_id"];
	    $deresult=$deresultArr["cmd_result"];
	    $detime=$deresultArr["time"];
//	    $string = "\n......device_id:".$deval."trans_id:".$detrans."cmd_result:".$deresult."detime:".$detime."\n";
//        file_put_contents("test.log", $string, FILE_APPEND);
	    if(!$deval || !$detrans || !$deresult || !$detime || $deresult=="ERROR")
	   {		       		
//		    $string = "\n device_id:".$deval."deip:".$deip."deport".$deport."detime".$detime."\n";
//          file_put_contents("test.log", $string, FILE_APPEND);
            return;
	    }else if($deresult=="OK")
        {    
		    $myconn=@mysql_connect("localhost","root","root");	     
            if(!$myconn)
            {
                die('Could not connect: '.mysql_error());
			    $string = "Could not connect: ".mysql_error();
                file_put_contents("test.log", $string, FILE_APPEND);
			    echo "{\"result\":\"ERROR\"}";
			    return;
            }
		
	        $db_selected=mysql_select_db("dwaccessDB",$myconn);
	        if(!$db_selected)
            {
                die("Can't use table : ".mysql_error());
	            $string = "Can't use table : ".mysql_error();
                file_put_contents("test.log", $string, FILE_APPEND);
			    echo "{\"result\":\"ERROR\"}";
			    return;
            }
		
   		    $strSql="DELETE FROM cmdform WHERE transid = \"".$detrans."\";";		
		    $result=mysql_query($strSql,$myconn);
		
//		    $string = "\n strSql:".$strSql."result : ".$dbResult[0]."\n";
//    		file_put_contents("test.log", $string, FILE_APPEND);
        
		    @mysql_free_result($result);
	        mysql_close($myconn);
//		    echo "{\"result\":\"OK\"}";
//		    return;        	 		
	    }else
	    {
//	        file_put_contents("test.log", " _POST[] null \n", FILE_APPEND);
//		    echo "{\"result\":\"ERROR\"}";
//		    return;	 
	    }
	    $indexA=strpos(substr(indexA,1),'{');
		$indexB=strpos(substr(indexB,1),'}');
		$deresult=substr($deresult,$indexB);
	}
?> 
